package jgamio.framework.examples.example01;

import static org.lwjgl.opengl.GL11.*;

import org.lwjgl.input.Keyboard;

import jgamio.framework.application.BasicApplication;
import jgamio.framework.application.Component;
import jgamio.framework.application.EnumDispatcher;
import jgamio.framework.application.KeyboardConfigLoader;
import jgamio.framework.application.KeyboardHandler;
import jgamio.framework.application.KeyboardHandler.KeyEvent;
import jgamio.framework.application.KeyboardHandler.KeyPressType;
import jgamio.framework.application.KeyboardHandler.Listener;
import jgamio.framework.application.exceptions.DisplayNotAvailable;
import jgamio.framework.application.exceptions.EnumDispatcherNotDefined;
import jgamio.framework.application.exceptions.InvalidConfigurationFile;

public class Application extends BasicApplication
{
	private int positionX = 0;
	
	public Application() throws DisplayNotAvailable, InvalidConfigurationFile, EnumDispatcherNotDefined
	{
		super(640, 480, "My first game", 1, 0, false);
	
//		this.keyboardHandler.addEvent(componentGame, Keyboard.KEY_LEFT, keyboardHandler.new KeyEvent(GameCommands.PLAYER_MOVE_LEFT), KeyPressType.REPEATED);
//		this.keyboardHandler.addEvent(componentGame, Keyboard.KEY_RIGHT, keyboardHandler.new KeyEvent(GameCommands.PLAYER_MOVE_RIGHT), KeyPressType.REPEATED);
//		this.keyboardHandler.addEvent(componentMenu, Keyboard.KEY_ESCAPE, keyboardHandler.new KeyEvent(MenuCommands.EXIT_APPLICATION), KeyPressType.SINGLE);

		this.addEnumDispatcher(componentMenu, MenuCommands.EXIT_APPLICATION);
		this.addEnumDispatcher(componentGame, GameCommands.PLAYER_MOVE_LEFT);
		
		this.loadKeyboardConfig("data/Keyboard.xml");
		this.setGameActive();

		this.keyboardHandler.addListener(new Listener()
		{
			@Override
			public void onKeyEvent(KeyEvent eventNumber)
			{
				if (isGameActive())
				{
					switch ((GameCommands) eventNumber.getValue())
					{
						case PLAYER_MOVE_LEFT:
							Application.this.positionX -= 10;
							break;
							
						case PLAYER_MOVE_RIGHT:
							Application.this.positionX += 10;
							break;
					}
				}
				else
				if (isMenuActive())
				{
					switch ((MenuCommands) eventNumber.getValue())
					{
						case EXIT_APPLICATION:
							System.exit(0);
							break;
					}
				}
			}
		});
		
		this.run();
	}
	
	@Override
	protected void handleGameEvent(KeyboardHandler.KeyEvent event)
	{
		System.out.println(event.getValue());
	}
	
	@Override
	protected void handleMenuEvent(KeyboardHandler.KeyEvent event)
	{
		System.out.println(event.getValue());
	}
	
	@Override
	protected void onRedraw()
	{
		glClear(GL_COLOR_BUFFER_BIT);
		
		glColor3f(1.0f, 1.0f, 1.0f);
		glPointSize(10.0f);
		glBegin(GL_POINTS);
			glVertex2i(positionX, 100);
		glEnd();
		
		glFlush();
	}
}
